<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Controller;
use think\Request;

class Cate extends Backend {
    public function __construct()
    {
        parent::__construct();
        $this->CateModel = model('Post.Cate');
    }

    public function index() {

        // 分类列表
        $CateList = $this->CateModel->order('id')->paginate(4, false, ['query' => Request::instance()->param()]);
        // 条件搜索
        if ($this->request->isGet()) {
            // $CateList = null;
            $action = $this->request->param('action');
            // $params = $this->request->param();
            // halt($params);
            if ($action == 'search') {
                $params = $this->request->param();
                // halt($params);
                if ($params['search'] == 'name') {
                    $CateList = $this->CateModel->where('name', 'LIKE','%'.$params['title'].'%')->paginate(4, false, ['query' => 
                    Request::instance()->param()]);
                } 
                if ($params['search'] == 'id') {
                    $CateList = $this->CateModel->where('id', 'LIKE', '%'.$params['title'].'%')->paginate(4, false, ['query' => 
                    Request::instance()->param()]);
                } 
            }
        }

        foreach($CateList as $item) {
            // halt($item['id']); // 分类id
            $res = model('Post.Post')->where('cateid', $item['id'])->select();
            // 获取对应帖子数量
            $num = count($res);
            // 添加属性
            $item['num'] = $num;
        }

        // 添加分类
        if ($this->request->isAjax()) {
            $action = $this->request->param('action');
            if ($action == 'InputChange') {
                $value = $this->request->param('value');
                // halt($value);
                $data = $this->CateModel->where('name', $value)->find();
                // halt($res);
                if ($data != null) {
                    $this->error('分类名称已存在', null, $data);
                }
                $this->success('分类名称可以使用', null, $data);
            }
            if ($action == 'addCate') {
                $value = $this->request->param('value');
                $data = $this->CateModel->where('name', $value)->find();
                if ($data != null) {
                    $this->error('分类名称已存在', null, $data);
                }
                // $this->success('分类名称可以使用', null, $data);
                $CateDate = [
                    'name' => $value,
                    'weigh' => 0
                ];
                $res = $this->CateModel->save($CateDate);
                if ($res == false) {
                    $this->error('添加分类失败');
                }
                $this->success('添加分类成功', url('admin/cate/index'));

            }
        }
        $navTitle = '问答管理';
        $navSmall = '分类列表';
        return $this->fetch('', [
            'navTitle' => $navTitle,
            'navSmall' => $navSmall,
            'CateList' => $CateList
        ]);
    }

    // 删除分类
    public function del() {
        $id = $this->request->param('id');
        $CateState  = $this->CateModel->find($id);
        if ($CateState == null) {
            $this->error('该分类无法删除');
        } 
        $res = $this->CateModel->where('id', $id)->delete();
        if ($res == false) {
            $this->error('删除分类失败');
        }
        $this->success('删除分类成功');
    }

    // 编辑分类
    public function edit() {
        if ($this->request->isAjax()) {
            $action = $this->request->param('action');
            // 点击获取数据
            if ($action == 'editCate') {
                $id = $this->request->param('id');
                $data = $this->CateModel->find($id);
                if ($data == null) {
                    $this->error('未知错误,无法修改分类名称');
                }
                $this->success('允许修改分类名称', null, $data);
            }
            // 确定修改
            if ($action == 'SaveEdit') {
                $saveid = $this->request->param('id');
                $value = $this->request->param('value');
                $CateState = $this->CateModel->where('name', $value)->find();
                if ($CateState != null) {
                    $this->error('分类名称已存在', null, $CateState);
                }
                $CateDate = [
                    'id' => $saveid,
                    'name' => $value,
                    'weigh' => 0
                ];
                // halt($CateDate);
                $res = $this->CateModel->isUpdate(true)->save($CateDate);
                if ($res == false) {
                    $this->error('更新分类失败');
                }
                $this->success('更新分类成功', url('admin/cate/index'));
            }
        }
    }

}
